package com.action.pc.user;

import com.bean.Sys_user;
import com.common.BaseAction;
import com.common.SystemConfig;
import com.service.user.UserService;
import com.util.DESUtils;
import com.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lvjianqing on 2017/9/9.
 * 登录相关
 */
@Controller
public class LoginAction extends BaseAction {

    Logger log = Logger.getLogger(LoginAction.class);

    @Autowired
    private UserService userService;

    /**
     * 跳转到登录页
     */
    @RequestMapping("login")
    public ModelAndView login(HttpServletRequest request, ModelMap modelMap, LoginVo vo) {
        modelMap.put("errMessage",vo.getErrMessage());
        return new ModelAndView("/pc/login", modelMap);
    }

    /***
     * 登录
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("dologin")
    public ModelAndView dologin(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, LoginVo vo) {
         String loginName = vo.getLoginName();
        String password = vo.getPassword();
        if (StringUtil.isEmpty(loginName) || StringUtil.isEmpty(password)) {
            modelMap.put("errMessage", "用户名或密码不存在");
            return new ModelAndView("redirect:/login.htm", modelMap);
        } else {
            try {
                Sys_user user = userService.getUserByLoginName(loginName);
                if (user == null) {
                    modelMap.put("errMessage", "用户不存在");
                    return new ModelAndView("redirect:/login.htm", modelMap);
                } else {
                    if (!DESUtils.encrypt(password).equals(user.getPassword())) {
                        modelMap.put("errMessage", "密码不正确");
                        return new ModelAndView("redirect:/login.htm", modelMap);
                    }else{
                        //登录成功
                        userService.loadUserMenu(request,user); //加载当前登录的用户所有的菜单
                    }
                }
            } catch (Exception ex) {
                log.error(ex.toString());
                ex.printStackTrace();
                return new ModelAndView("redirect:/login.htm", modelMap);
            }
        }
        return new ModelAndView("redirect:/index.htm", modelMap);
    }

    /**
     * 退出登录
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("loginout")
    public ModelAndView loginout(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, LoginVo vo) {
        request.getSession().removeAttribute(SystemConfig.XtcdHasMapSessionName);
        request.getSession().removeAttribute(SystemConfig.ManagerSessionName);
        return new ModelAndView("redirect:/login.htm", modelMap);
    }
}
